﻿using System;
using System.Net;
using FlashCards.SL.FlashCardRepeater.Utils.Interfaces;
using FlashCards.Dto;
using FlashCards.SL.FlashCardRepeater.ViewModels;
using System.Collections.Generic;

namespace FlashCards.SL.FlashCardRepeater.Utils
{
    /// <summary>
    /// Podstawowy selektor.
    /// Bierze fiszki po kolei, a kiedy dojdzie do końca kolekcji zaczyna od początku.
    /// </summary>
    public class BasicFlashcardSelector : IFlashcardsSelector
    {
        private List<FlashcardViewModel> _flashcards;
        private FlashcardViewModel _currentFlashcard;

        public BasicFlashcardSelector(List<FlashcardViewModel> flashcards)
        {
            _flashcards = flashcards;
        }


        public FlashcardViewModel GetNext()
        {
            if (_currentFlashcard == null)
            {
                _currentFlashcard = _flashcards[0];
                return _currentFlashcard;
            }
            else
            {
                int index = _flashcards.IndexOf(_currentFlashcard);
                if (index == _flashcards.Count - 1)
                    _currentFlashcard = null;
                else
                    _currentFlashcard = _flashcards[index + 1];

                return _currentFlashcard;
            }
        }


        public int GetFlashcardsUntilEnd()
        {
            int cardsCount = _flashcards.Count;
            int untilEndCount = cardsCount;
            if (_currentFlashcard != null)
            {
                var index = _flashcards.IndexOf(_currentFlashcard);
                untilEndCount -= index + 1;
            }
            return untilEndCount;
        }
    }
}
